<?php


namespace app\shop\model;


use app\BaseModel;

class AdminParameter extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_goods_parameter';
    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    /**
     * @param $where
     * @param int $page
     * @功能说明:获取参数列表
     * @author lxj
     * @DataTime: 2020/5/18 14:07
     */
    public function getList($where,$page=10){
        $list = $this->where($where)->paginate($page)->each(function($item, $key){
            if($item['pid']==0){
                $item['level'] = '一级';
            }else{
                $item['level'] = '二级';
            }
        });
        return $list;
    }

    /**
     * @param $where
     * @功能说明:获取参数数组，下拉使用
     * @author lxj
     * @DataTime: 2020/5/19 10:58
     */
    public function getAdminList($where){
        $list = $this->where($where)->field('id,name')->select()->toArray();
        return $list;
    }

    /**
     * @param $input
     * @功能说明:商品参数返回数据
     * @author lxj
     * @DataTime: 2020/5/18 14:26
     */
    public function returnData($input){
        $data['name']       = $input['name'];
        $data['pid']        = $input['pid']??0;
        $data['status']     = $input['status'];
        return $data;
    }

    /**
     * @param $data
     * @功能说明:商品参数新增
     * @author lxj
     * @DataTime: 2020/5/18 14:38
     */
    public function parameterAdd($data){
        //判断是否参数名称是否重复
        $where=['pid'=>$data['pid'],'name'=>trim($data['name']),'uniacid'=>$data['uniacid']];
        $is_repeat = $this->where($where)->where('status','<>',-1)->find();
        if($is_repeat){
            return false;
        }else{
            $data['create_time'] = time();
            $res = $this->insertGetId($data);
            return $res;
        }
    }

    /**
     * @param $data
     * @param $id
     * @param $type
     * @功能说明:商品参数修改
     * @author lxj
     * @DataTime: 2020/5/18 14:44
     */
    public function parameterUpdate($id,$data,$type=1){
        if($type==2){
            //判断是否参数名称是否重复
            $where2=['name'=>trim($data['name']),'uniacid'=>$data['uniacid'],'pid'=>$data['pid']];
            $is_repeat = $this->where($where2)
                ->where('status','<>',-1)->where('id','<>',$id)->find();
            if($is_repeat){
                return false;
            }
        }
        $data['update_time'] = time();
        //修改当前数据信息
        $res = $this->where('id',$id)->update($data);
        //查询下级数据 修改下级数据状态
        $child_id = $this->where('pid',$id)->where('status','<>',-1)->column('id');
        if($child_id){
            unset($data['pid']);
            unset($data['name']);
            $res = $this->where('id','in',$child_id)->update($data);
        }
        return $res;
    }
    /**
     * @param $where
     * @功能说明:查询当前参数信息
     * @author lxj
     * @DataTime: 2020/5/18 14:43
     */
    public function parameterInfo($where){
        $data = $this->where($where)->find();
        $data = !empty($data)?$data->toArray():$data;
        return $data;
    }
    /**
     * @param $parameter_id
     * @功能说明:删除该参数的子参数，使用该参数的模板关联
     * @author lxj
     * @DataTime: 2020/5/18 14:43
     */
    public function delParameter($parameter_id,$uniacid){
        $child_id = $this->where('pid',$parameter_id)->column('id');
        if($child_id){
            //删除下级参数
            $res =$this->where('id','in',$child_id)->update(['status'=>-1]);

            $child_id[]=$parameter_id;
            //删除模板关系
            $res =AdminParameterTemplateInfo::where('parameter_id','in',$child_id)->where('uniacid',$uniacid)
                ->where('status','<>',-1)->update(['status'=>-1]);
        }else{
            //删除该参数的模板关系
            $res =AdminParameterTemplateInfo::where('parameter_id',$parameter_id)->where('uniacid',$uniacid)
                ->where('status','<>',-1)->update(['status'=>-1]);
        }
        return $res;
    }
}